package cn.wolfcode.service.impl;

import cn.wolfcode.domain.SystemDictionary;
import cn.wolfcode.mapper.SystemDictionaryMapper;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.ISystemDictionaryService;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class SystemDictionaryServiceImpl implements ISystemDictionaryService {

    @Autowired
    private SystemDictionaryMapper systemDictionaryMapper;

    @Override
    @Transactional
    public void save(SystemDictionary systemDictionary) {
        //调mapper接口中的方法
        systemDictionaryMapper.insert(systemDictionary);
    }

    @Override
    @Transactional
    public void delete(Long id) {
        systemDictionaryMapper.deleteByPrimaryKey(id);
    }

    @Override
    @Transactional
    public void update(SystemDictionary systemDictionary) {
        systemDictionaryMapper.updateByPrimaryKey(systemDictionary);
    }

    @Override
    public SystemDictionary get(Long id) {
        return systemDictionaryMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<SystemDictionary> listAll() {
        return systemDictionaryMapper.selectAll();
    }

    @Override
    public PageInfo<SystemDictionary> query(QueryObject qo) {
        //定义查询页,每页页数
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<SystemDictionary> list = systemDictionaryMapper.selectForList(qo);
        return new PageInfo<>(list);
    }

    @Override
    public List<Map> queryTreeData() {
         //查询顶级数据目录（父节点）-parent_id 为null
        List<SystemDictionary> all = systemDictionaryMapper.selectAll();
        List<SystemDictionary> top = new ArrayList<>();

        for (SystemDictionary item : all) {
            if(item.getParentId()==null){
                top.add(item);   //将查出来的item存入top中、
                item.setChildren(findChildren(all,item.getId()));
            }
        }

        //查询二级（下级）数据路径，查询儿子（子节点）


        fillChildren(all,top);


        //页面的treeview用到属性：text和nodes
        //而查询出来的属性是name和children
        //思考：如何进行转化？

        //方案1：使用map进行数据改造[推荐]
        String str = JSON.toJSONString(top);  //把对象转换为字符串
        str=str.replaceAll("name","text").replaceAll("children","nodes");

        //参数1：list集合JSON格式的字符串，参数2：集合泛型
        List<Map> maps = JSON.parseArray(str, Map.class);  //把json格式的自符串转换为对象

        //方案2：修改数据字典（不建议）

        return maps;
    }

    //填充儿子
    private void fillChildren(List<SystemDictionary> all, List<SystemDictionary> children) {
        //判空
        if (children==null||children.size()==0){
            return;
        }

        for (SystemDictionary item : children) {
            //找儿子
            item.setChildren(findChildren(all,item.getId()));
            //儿子找儿子(开始递归操作)
            fillChildren(all,item.getChildren());

        }
    }

    /*
    * 参数1：所有查询的数据，参数2：已经查询出来的父id
    */
    private List<SystemDictionary> findChildren(List<SystemDictionary> all,Long parentId){
        List<SystemDictionary> children = new ArrayList<>();

        for (SystemDictionary item : all) {
            if(item.getParentId()==parentId){  //此时查询出来节点的parentId与传如入的父id相同，故此节点为父节点的儿子
                children.add(item);
            }
        }
        return children;
    }

    @Override
    public List<SystemDictionary> queryBySn(String sn) {
        return systemDictionaryMapper.selectBySn(sn);
    }

    @Override
    public List<SystemDictionary> queryByParentId(Long parentId) {
        return systemDictionaryMapper.selectByParentId(parentId);
    }

}
